﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;
using MyToolkit.Com;

namespace MyToolkit
{
    public partial class FrmMain : Form
    {
        private int winTempldateEditCount = 0;

        private FrmDatabaseExplorer _frmDataBaseExp;
        private FrmResourceExplorer _frmResourceExp;
        private FrmOutput _frmOutput;

        public FrmMain()
        {
            InitializeComponent();
            //添加对像浏览器
            AddViewDatabaseExplorer();
        }


        private void NewTemplateEdit(string content, string frmText = "")
        {
            var frm = new FrmTemplateEdit(content);
            frm.Text = String.IsNullOrEmpty(frmText) ? frm.Text + "_" + (++winTempldateEditCount) : frmText;
            frm.Show(dockPanel1);
        }

        private void OpenTemplateEdit()
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var stream = new StreamReader(openFileDialog1.FileName))
                {
                    var strContent = stream.ReadToEnd();
                    var fileInfo = new FileInfo(openFileDialog1.FileName);
                    NewTemplateEdit(strContent, fileInfo.Name);
                }
            }
        }

        private void SaveTemplateEdit()
        {
            var frm = dockPanel1.ActiveDocument as FrmTemplateEdit;
            saveFileDialog1.FileName = frm.Text;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (var stream = new StreamWriter(saveFileDialog1.FileName))
                {
                    stream.Write(frm.EditText);
                }
            }
        }


        protected void AddViewDatabaseExplorer()
        {
            if (_frmDataBaseExp == null)
            {
                _frmDataBaseExp = new FrmDatabaseExplorer();
                _frmDataBaseExp.Name = "frmDataBaseExp";
                _frmDataBaseExp.OnClickMenuEvent += _frmDataBaseExp_OnClickMenuEvent;
            }
            _frmDataBaseExp.Show(dockPanel1, DockState.DockLeft);
        }

        void _frmDataBaseExp_OnClickMenuEvent(object sender, DatabaseExplorerEventArgs e)
        {
            switch (e.MenuID)
            { 
                case 1 :
                    var frmEntityBuilder = new FrmEntityBuilder(e.Connection, e.ObjectText);
                    frmEntityBuilder.Show(dockPanel1);
                    ShowResourceExplorer();
                    ShowOutput();
                    break;
            }
        }


        private void miShuJuKuLiuLanQi_Click(object sender, EventArgs e)
        {
            AddViewDatabaseExplorer();
        }

        private void miXinJianMoBan_Click(object sender, EventArgs e)
        {
            NewTemplateEdit(String.Empty);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            NewTemplateEdit(String.Empty);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            OpenTemplateEdit();
        }

        private void miDaKaiMoBan_Click(object sender, EventArgs e)
        {
            OpenTemplateEdit();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SaveTemplateEdit();
        }

        private void miSave_Click(object sender, EventArgs e)
        {
            SaveTemplateEdit();
        }

        private void dockPanel1_ActiveDocumentChanged(object sender, EventArgs e)
        {
            if (dockPanel1.ActiveDocument is FrmTemplateEdit)
            {
                miSave.Enabled = true;
                toolStripButton3.Enabled = true;
            }
            else
            {
                miSave.Enabled = false;
                toolStripButton3.Enabled = false;
            }

            if (dockPanel1.ActiveDocument is FrmEntityBuilder)
            {
                toolStrip2.Visible = true;
            }
            else
            {
                toolStrip2.Visible = false;
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //var edit = dockPanel1.ActiveDocument as FrmTemplateEdit;

            //var t4 = new T4Output(edit.EditText);

            //MessageBox.Show(t4.Generation(new DbTable() { Name = "aaa", ObjectID = 12312 }));
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {

        }

        private void miWebJianLiuLanQi_Click(object sender, EventArgs e)
        {
            ShowResourceExplorer();   
        }

        private void ShowResourceExplorer()
        {
            if (_frmResourceExp == null)
            {
                _frmResourceExp = new FrmResourceExplorer();
                _frmResourceExp.Name = "frmResourceExp";
                _frmResourceExp.OnClickFileEvent += _frmResourceExp_OnClickFileEvent;
            }
            _frmResourceExp.Show(dockPanel1, DockState.DockRight);
        }

        void _frmResourceExp_OnClickFileEvent(object sneder, ResourceExplorerEventArgs e)
        {
            FileInfo file = new FileInfo(e.FullName);
            if (file.Extension == ".tt")
            {
                var content = String.Empty;
                using (var stream = new StreamReader(file.FullName))
                {
                    content = stream.ReadToEnd();
                }
                if (dockPanel1.ActiveDocument is FrmEntityBuilder)
                {
                    var frm = dockPanel1.ActiveDocument as FrmEntityBuilder;
                    frm.SetTemplateContent(content);
                }
                else
                {
                    NewTemplateEdit(content, e.FileName);
                }
            }
        }

        private void miShuChu_Click(object sender, EventArgs e)
        {
            ShowOutput();
        }

        private void ShowOutput()
        {
            if (_frmOutput == null)
            {
                _frmOutput = new FrmOutput();
                _frmOutput.Name = "frmOutput";

            }
            _frmOutput.Show(dockPanel1, DockState.DockBottom);
        }
    }
}
